﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using AutonomicComputingLibrary.MAPE;

namespace AutonomicComputingLibrary.CHOP.CheckUP
{
    class DiskManagement // : AutonomicComputingLibrary.ICheckUp
    {
        //do not use singleton on this so they can monitor each hd individually


          public double TotalMB { get; set; }

        private string DriveLetter = "C";

        public int UsagePercentage { get; set; }

        public int PercentageMin { get; set; }

        public int PercentageMax { get; set; }

        public double GetTotalHDDSize(string driveLetter)
        {
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                if (drive.IsReady && drive.Name == driveLetter)
                {
                    return drive.TotalSize / Math.Pow(1024, 3);
                }
            }
            return -1;
        }

        public double GetTotalHDDFreeSpace(string driveLetter)
        {
            foreach (var drive in DriveInfo.GetDrives().Where(drive => drive.IsReady && drive.Name == driveLetter))
            {
                return drive.AvailableFreeSpace / Math.Pow(1024, 3);
            }
            return -1;
        }

        public DiskManagement(string DriveLetter, int Percentagemax, int Percentagemin = 50)
        {

            PercentageMin = Percentagemin;
            PercentageMax = Percentagemax;
            this.DriveLetter = DriveLetter;

            TotalMB = GetTotalHDDSize(DriveLetter);
        }

        public int disable
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string GetAlert()
        {
            throw new NotImplementedException();
        }

        public int pause
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }


        public event AlertHandler Alert;

        public void CheckUp()
        {


            while (true)
            {
                System.Threading.Thread.Sleep(3000);
                if (Check() == Health.Negative)
                {
                    ACLAlert aclal = new ACLAlert();
                    aclal.Alert = Health.Negative.ToString();
                    Alert(this, aclal);


                }
            }
        }

        public Health? Check()
        {
            try
            {
                var space = GetTotalHDDFreeSpace(DriveLetter);

               return space > PercentageMax || space < PercentageMin
                           ? Health.Negative
                           : Health.Positive;
            }
            catch
            {
                return Health.UnableToDetermine;
            }
        }

        public void GetHealthHistory()
        {
            throw new NotImplementedException();
        }
    }


}
